
//jenkins流水线中的Build Steps中执行： groovy himarttest/jenkins/himart-test.groovy  //注意himarttest是远程仓库中的项目名
pipeline {
    agent any
    environment {

        base_url='http://139.159.214.157'
        username='shuwenouwan'
        password='Shuwenouwan123'
        // 测试报告
        alluredir = '/reports/allure/allure-results'
        allure_reports = '/reports/allure/allure-reports'
        allure_reports_url = '139.159.214.157'
        allure_reports_url_port = '8083'

        //测试标记为regression、function、compatibility的用例
        // markers= '["function","regression","compatibility"]' 
        markers= '["regression"]' 

        // 项目地址
        project_url = 'https://shuwenouwan:Shuwenouwan123@gitee.com/shuwenouwan/himart-test.git'



        // platform_brand_model = '''{
        //   "Android": {
        //     "小米": ["xiaomi 6", "xiaomi 10", "xiaomi 13"],
        //     "华为": ["huawei P40", "huawei Mate 40 Pro", "huawei Nova 9"],
        //     "三星": ["samsung Galaxy S21", "samsung Galaxy Note 20", "samsung Galaxy Z Fold 3"],
        //     "OPPO": ["oppo Find X3 Pro", "oppo Reno6 Pro+", "oppo A93"],
        //     "vivo": ["vivo X60 Pro", "vivo Y76s", "vivo iQOO Neo5"]
        //   },
        //   "ios": {
        //     "iPhone": ["iPhone 11", "iPhone 12 Pro Max", "iPhone SE (2nd generation)"],
        //     "iPad": ["iPad Pro (12.9-inch)", "iPad Air (4th generation)", "iPad mini (6th generation)"]
        //   }
        // }'''

        // Android/ios平台
        platform_brand_model = '''{
          "Android": {
            "小米": ["xiaomi 11"]
          }
        }'''
        automation_name = 'UiAutomator2'
        webdriver_url_appium = "localhost:4273"
        app_url = '["himart-test/resources/app-file/himart.apk", "himart-test/resources/app-file/himart.ipa"]' 
        device_name = '["device_functional_tests", "device_integration_tests", "device_compatibility_tests"]' 
        app_package_name = '["uni.UNI31459F4", "uni.UNI31459F4"]' 
        app_activity_name = '["io.dcloud.PandoraEntry", "io.dcloud.PandoraEntry"]' 
        architecture = '["x86_64", "arm64-v8a"]' 

        // web平台
        webdriver_url_selenium = "${base_url}:8444/wd/hub"
        // browsers = '["chrome", "edge", "firefox"]' 
        browsers = '["chrome"]' 
        headless = false
        // 小程序
    }    
    stages {
        stage('构建安装检查测试环境'){
            steps {
                script {
                    //更新容器中的himart-test测试项目文件
                    // sh "git clone https://shuwenouwan:Shuwenouwan123@gitee.com/shuwenouwan/himart-test.git"
                    // sh "git config --global http.postBuffer 209715200"
                    // sh "git config --global user.name 'shuwenouwan'" 
                    // sh "git config --global user.email '2509611050@qq.com'"
                    sh "rm -rf himart-test && git clone https://${username}:${password}@gitee.com/shuwenouwan/himart-test.git"
                    
                    //在当前容器路径下执行：容器尚未启动则启动;已启动无更新则不变;已经运行有更新则更新
                    // sh "docker-compose -f himart-test/Docker/docker-compose.selenium_grid.yml up -d"

                    //搭建nginx服务器展示报告
                    // sh "docker build -t nginx_images  -f himart-test/Docker/Dockerfile.nginx ."
                    // nginx_container = sh(returnStdout: true, script: "docker ps -a --filter=name='nginx_container' --format '{{.ID}}'").trim()
                    // if  (!nginx_container.isEmpty()) {
                    //    sh "docker stop  nginx_container && docker rm nginx_container"
                    // } 
                    // sh "docker run  -v /root/dist:/usr/share/nginx  -d -p 8040:8040 --name=nginx_container nginx_images"
                    
                    // // 启动nginx服务容器
                    sh "docker-compose -f himart-test/Docker/docker-compose.nginx.yml up -d"
                    // // 启动mall项目相关服务容器(包含前台和后台系统)
                    sh "docker-compose -f himart-test/Docker/docker-compose.mall.yml up -d"
                    // // 启动mobsf安全测试服务容器
                    // sh "docker-compose -f himart-test/Docker/docker-compose.mobsf.yml up -d"
                    // // 启动sonarqube代码检查服务容器
                    sh "docker-compose -f himart-test/Docker/docker-compose.sonarqube.yml up -d"

                    // sh "docker-compose -f himart-test/Docker/docker-compose.nginx.yml down"
                    // sh "docker-compose -f himart-test/Docker/docker-compose.mall.yml down"
                    // sh "docker-compose -f himart-test/Docker/docker-compose.mobsf.yml down"

                    //手动启动或停止服务
                    // docker-compose -f /var/jenkins_home/workspace/himarttest/himart-test/Docker/docker-compose.nginx.yml up -d
                    // docker-compose -f /var/jenkins_home/workspace/himarttest/himart-test/Docker/docker-compose.mall.yml up -d
                    // docker-compose -f /var/jenkins_home/workspace/himarttest/himart-test/Docker/docker-compose.mobsf.yml up -d

                    // docker-compose -f /var/jenkins_home/workspace/himarttest/himart-test/Docker/docker-compose.nginx.yml down
                    // docker-compose -f /var/jenkins_home/workspace/himarttest/himart-test/Docker/docker-compose.mall.yml down
                    // docker-compose -f /var/jenkins_home/workspace/himarttest/himart-test/Docker/docker-compose.mobsf.yml down




                    // 搭建项目后端接口服务
                    // sh "docker build -t mall_admin_images  -f himart-test/Docker/Dockerfile.mall_admin ."
                    // mall_admin_container = sh(returnStdout: true, script: "docker ps -a --filter=name='mall_admin_container' --format '{{.ID}}'").trim()
                    // if  (!mall_admin_container.isEmpty()) {
                    //    sh "docker stop  mall_admin_container && docker rm mall_admin_container"
                    // } 
                    // sh "docker run  -v /root/dist:/app  -d -p 8084:8080 --name=mall_admin_container mall_admin_images"
                    
                    // 搭建项目前端接口服务
                    // sh "docker build -t mall_portal_images  -f himart-test/Docker/Dockerfile.mall_portal ."
                    // mall_portal_container = sh(returnStdout: true, script: "docker ps -a --filter=name='mall_portal_container' --format '{{.ID}}'").trim()
                    // if  (!mall_portal_container.isEmpty()) {
                    //    sh "docker stop  mall_portal_container && docker rm mall_portal_container"
                    // } 
                    // sh "docker run  -v /root/dist:/app  -d -p 8085:8085 --name=mall_portal_container mall_portal_images"

                    // 构建newman镜像
                    // sh "docker build -t newman_image -f himart-test/Docker/Dockerfile.newman ."
                    // 构建jmeter镜像
                    // sh "docker build -t jmeter_image -f himart-test/Docker/Dockerfile.jmeter ."
                    // 构建allure镜像
                    // sh "docker build -t pytest_allure_image -f himart-test/Docker/Dockerfile.pytest_allure ."

                }
            }
        }
        stage('并行测试') {
            parallel {
                stage('web平台:功能性测试|兼容性测试|回归测试') {
                    steps {
                        script {
                            sh "echo 'mobile平台测试'"
                            // def browsers = env.browsers.replaceAll('["\\[\\]]', '').split(', ')  // 移除引号和方括号，并分割字符串
                            // def markers = env.markers.replaceAll('["\\[\\]]', '').split(', ')  // 移除引号和方括号，并分割字符串
                            // for (marker in markers) { 
                            //     sh "echo '当前测试：'${marker}"
                            //     for (browser in browsers) { //【并行测试chrome、edge、Firefox】
                            //         sh "echo '当前测试：'${browser}"
                            //         def currentTime = new Date()
                            //         def formattedTime = currentTime.format("yyyy_MM_dd_HH_mm_ss")
                            //         def containerName = "test_${marker}_${browser}_${formattedTime}"
                            //         sh """
                            //             docker run  -v ${alluredir}:${alluredir} -v ${allure_reports}:${allure_reports} -d -p 8010:8010 --name=${containerName} pytest_allure_image &&
                            //             docker exec -t ${containerName} bash -c 'rm -rf himart-test' &&
                            //             docker exec -t ${containerName} bash -c 'git clone https://${username}:${password}@gitee.com/shuwenouwan/himart-test.git' &&
                            //             docker exec -t ${containerName} bash -c 'pytest  -m ${marker} --alluredir=${alluredir} --allure_reports=${allure_reports}  --allure_reports_url=${allure_reports_url}  --allure_reports_url_port=${allure_reports_url_port}   --webdriver_url_selenium=${webdriver_url_selenium}   --browser=${browser}    himart-test/tests/web_tests/tests/functional_tests/test_login3.py' &&
                            //             docker exec -t ${containerName} bash -c 'allure generate /reports/allure/allure-results  -o /reports/allure/allure-report/' &&
                            //             docker stop  ${containerName} && docker rm  ${containerName} 
                            //         """                                                                                                                
                            //     }
                            // }
                        }
                    }
                }
                stage('mobile:功能性/兼容性/回归测试') {
                    steps {
                        echo "mobile."
                        script {
                            sh "echo 'mobile平台测试'"
                            // //更新容器中的himart-test测试项目文件
                            // sh "docker exec appium_android rm -rf himart-test && docker exec appium_android git clone https://${username}:${password}@gitee.com/shuwenouwan/himart-test.git"
                            // // 每个操作系统下每个品牌手机下每个型号并行测试
                            // def mobileBrands = groovy.json.JsonSlurper.class.newInstance().parseText(env.platform_brand_model)
                            // mobileBrands.each { os, brands ->
                            //     println "当前测试：Operating System: ${os}"
                            //     brands.each { brandName, models ->
                            //         println "当前测试：Brand: ${brandName}"
                            //         models.each { model ->
                            //             println "当前测试：Model: ${model}"
                            //             sh "docker exec appium_android  pytest_allure  pytest -m ${marker} --webdriver_url_appium=${webdriver_url_appium}  --app_activity_name=${app_activity_name[0]} --app_package_name=${app_package_name[0]}  --device_name=${device_name[0]}  --app_url=${app_url[0]}  --automation_name=${automation_name}  --platform_name=${platform_name} --alluredir=${alluredir} --allure_reports=${allure_reports}  --allure_reports_url=${allure_reports_url}  --allure_reports_url_port=${allure_reports_url_port}     /app/himart-test/tests/web_tests/tests/functional_tests/test_login_ok.py" 
                            //         }
                            //     }
                            // }

                            //调试v1： pytest --webdriver_url_appium=192.168.31.118:4723  --app_activity_name=io.dcloud.PandoraEntry --app_package_name=com.cloudx.himart  --device_name=192.168.31.11:5555 --app_url=D:\project\vscode\himart-test\tests\mobile_tests\resources\himart.apk  --automation_name=UiAutomator2  --platform_name=Android --alluredir=D:\download\reports\allure\allure-results --allure_reports=D:\download\reports\allure\allure-reports  --allure_reports_url=139.159.214.157  --allure_reports_url_port=8010     D:\project\vscode\himart-test\tests\mobile_tests\tests\functional_tests\test_login01.py
                            //调试v2： pytest --webdriver_url_appium=192.168.31.118:4723  --app_activity_name=io.dcloud.PandoraEntry --app_package_name=com.cloudx.himart  --device_name=192.168.31.11:5555 --app_url=D:\project\vscode\himart-test\tests\mobile_tests\resources\himart.apk  --automation_name=UiAutomator2  --platform_name=Android --alluredir=D:\download\reports\allure\allure-results --allure_reports=D:\download\reports\allure\allure-reports  --allure_reports_url=139.159.214.157  --allure_reports_url_port=8010     D:\project\vscode\himart-test\tests\mobile_tests\tests\functional_tests\test_login.py
                        }
                    }
                }
                stage('Api接口测试') {
                    steps {
                        script {
                            sh "echo 'Api接口测试'"
                            // def api_collection="/himart-test/tests/api_tests/newman_tests/api_collection_test.json"
                            // def formattedTime = new Date().format("yyyy_MM_dd_HH_mm_ss")
                            // def container_newman = "test_newman_${formattedTime}"
                            // sh """
                            //     docker run  -v /root/dist:/app -d --name=${container_newman} newman_image 
                                
                            //     docker exec -t ${container_newman} sh -c 'rm -rf himart-test  &&  git clone ${project_url}' 
                                
                            //     docker exec -t ${container_newman} sh -c 'cd /app  && newman run ${api_collection} \
                            //     --reporters html --reporter-html-export /app/index.html' 
                                    
                            //     docker exec -t ${container_newman} sh -c 'newman run ${api_collection} \
                            //     --reporters allure   --reporter-allure-export /app/allure-results'   
                                
                            //     docker stop  ${container_newman} 
                                
                            //     docker rm  ${container_newman} 
                            // """
                        }
                    }
                }
                stage('压力测试') {
                    steps {
                        script {
                            sh "echo '压力测试'"
                            // def formattedTime = new Date().format("yyyy_MM_dd_HH_mm_ss")
                            // def container_jmeter = "test_jmeter_${formattedTime}"
                            // sh """
                            //     docker run -v /root/dist:/app -d  --name=${container_jmeter}  jmeter_image &&
                            //     docker exec -t ${container_jmeter} sh -c 'rm -r /app/himart-test && git clone https://${username}:${password}@gitee.com/shuwenouwan/himart-test.git /app' &&
                            //     docker exec -t ${container_jmeter} sh -c 'cd /jmeter && jmeter -n -t /app/himart-test/tests/stress_tests/jmeter_tests/test-plan.jmx -l /jmeter-results.csv  -e -o /jmeter/jmeter-reports' &&
                            //     docker stop  ${container_jmeter} && docker rm  ${container_jmeter} 
                            // """
                        }
                    }
                }
            }
        }
        stage('生成报告') {
                steps {
                    sh "echo '生成报告'"
                    script {
                        sh "echo '生成报告'"

                        //  生成报告
                        // sh "docker run -v /root/dist:/reports -d \
                        //  --name=pytest_allure_container pytest_allure_image \
                        //  allure generate ${alluredir}  -o ${allure_reports}  --clean"  

                        // sh "docker exec -t pytest_allure_containe sh -c  \
                        // 'allure generate /reports/newman-result -o /reports/newman-reports  --clean'"       

                        // 查看生成html报告
                        // sh "docker run -v ${alluredir}:${alluredir} -v ${allure_reports}:${allure_reports} -d -t pytest_allure_image allure serve --port 8030 --host ${base_url}  ${alluredir}"
                                             
                    }
                }
        }
    }
}











